﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class SellerViewBuyer : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

           
                try
                {
                    SqlCommand cmdTransactionHistory, cmdTransactionHistory0;
                    SqlDataReader dtrTransactionHistory, dtrTransactionHistory0;
                    conn.Open();

                    cmdTransactionHistory = new SqlCommand("SELECT * from Transactions t, Users u WHERE t.u_id=u.u_id AND t.t_seller=@UID AND (t_status=@TheFirstStatus OR t_status=@TheSecondStatus)", conn);
                    cmdTransactionHistory.Parameters.AddWithValue("@UID", Session["ID"].ToString());
                    cmdTransactionHistory.Parameters.AddWithValue("@TheFirstStatus", "CartCompleted");
                    cmdTransactionHistory.Parameters.AddWithValue("@TheSecondStatus", "BuyNowCompleted");


                    dtrTransactionHistory = cmdTransactionHistory.ExecuteReader();


                    ItemList.DataSource = dtrTransactionHistory;
                    ItemList.DataBind();

                    dtrTransactionHistory.Close();

                    cmdTransactionHistory0 = new SqlCommand("SELECT * from Transactions t, Users u WHERE t.u_id=u.u_id AND t.t_seller=@UID AND (t_status=@TheThirdStatus OR t_status=@TheFourthStatus)", conn);
                    cmdTransactionHistory0.Parameters.AddWithValue("@UID", Session["ID"].ToString());
                    cmdTransactionHistory0.Parameters.AddWithValue("@TheThirdStatus", "CartShipped");
                    cmdTransactionHistory0.Parameters.AddWithValue("@TheFourthStatus", "BuyNowShipped");

                    dtrTransactionHistory0 = cmdTransactionHistory0.ExecuteReader();


                    ItemList0.DataSource = dtrTransactionHistory0;
                    ItemList0.DataBind();

                    dtrTransactionHistory0.Close();


                    conn.Close();
                }
                catch (SqlException ex)
                {
                    MessageBox(ex.Message);
                }
          
        }

        protected String formatRef(object tid)
        {
            String reference = "REF-" + String.Format("{0:00000}", int.Parse(tid.ToString()));
            return reference;
        }

        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "View")
            {
                int tid = Convert.ToInt32(e.CommandArgument.ToString());
                Redirect("ReferencePrinting.aspx?id=" + tid);
            }
            else if (e.CommandName == "Shipped")
            {
                int tid = Convert.ToInt32(e.CommandArgument.ToString());

                SqlCommand cmdSearchStatus,cmdUpdateStatus;
                
                
                conn.Open();

                cmdSearchStatus = new SqlCommand("Select t_status from Transactions Where t_id=@TID",conn);
                cmdSearchStatus.Parameters.AddWithValue("@TID",tid);
             
                String Tstatus =  Convert.ToString(cmdSearchStatus.ExecuteScalar());


                if (Tstatus.Equals("CartCompleted"))
                {
                    cmdUpdateStatus = new SqlCommand("Update Transactions SET t_status=@TStatus Where t_id=@TID", conn);
                    cmdUpdateStatus.Parameters.AddWithValue("@TStatus","CartShipped");
                    cmdUpdateStatus.Parameters.AddWithValue("@TID", tid);

                    cmdUpdateStatus.ExecuteNonQuery();
                }
                else if (Tstatus.Equals("BuyNowCompleted"))
                {

                    cmdUpdateStatus = new SqlCommand("Update Transactions SET t_status=@TStatus Where t_id=@TID", conn);
                    cmdUpdateStatus.Parameters.AddWithValue("@TStatus", "BuyNowShipped");
                    cmdUpdateStatus.Parameters.AddWithValue("@TID", tid);

                    cmdUpdateStatus.ExecuteNonQuery();
                }

                conn.Close();

                RedirectSameLocation("SellerViewBuyer.aspx");

            }
        }

     

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.open('" + msg + "', '_blank');</script>";
            Page.Controls.Add(lbl);
        }

        private void RedirectSameLocation(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected String ShowStatus(object status)
        {
            String afterConversion;

            if (status.Equals("BuyNowCompleted") || status.Equals("CartCompleted"))
            {
                afterConversion = "Waiting To Be Ship";
            }
            else
            {
               afterConversion = "Already Shipped";
            }

            return afterConversion;
        }



    }
}